#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../game_type.h"
#include "../game_func.h"
#include "../game_api.h"
#include "../game_struct.h"
#include "hook.h"

struct DoorInfo {
	u16 x;//X坐标
	u16 y;//Y坐标
	u16 w;//宽度
	u16 h;//高度
	u32 Func;//进门执行函数
	u8  Go;//GO提示方向 0=上 1=左上
	u8  ChkHasItem;//判断有没有某道具
	u32 ChkScore;//判断分数
	u8 ChkLevel;//判断角色等级
	s8 ChkRole;//判断某角色是否存在
	u8 ChkPlayers;//判断玩家数
};

const short DoorTbl[200][4]= {
	{0, 0, 0, 0},
	{0x3C8, 0x181, 0x46, 0x28},
	{0x347, 0xF0, 0x24, 0x3A},
	{0x347, 0xF0, 0x24, 0x3A},
	{0x2E2, 0x7E, 0x50, 0xA},
	{0x4E2, 0x7E, 0x50, 0xA},
	{0x4E2, 0x7E, 0x50, 0xA},
	{0x7D0, 0x6E, 0x46, 0x96},
	{0x2DE, 0x98, 0x2B, 0x32},
	{0x40, 0x1BA, 0x36, 0xC},
	{0x2DE, 0x98, 0x2B, 0x32},
	{0x2DE, 0x98, 0x2B, 0x32},
	{0x182, 0xF4, 0x50, 0xC},
	{0x182, 0xF4, 0x50, 0xC},
	{0, 0x7F, 0x32, 0x80},
	{0x89, 0xDF, 0x28, 0x17},
	{0x19B, 0xAC, 0x3C, 0xC},
	{0x40, 0x1BA, 0x36, 0xC},
	{0x460, 0xB3, 0x46, 0x12},
	{0x460, 0x6E, 0x46, 0x50},
	{0x7BA, 0xB4, 0x46, 0x50},
	{0x78, 0x50, 0x50, 0x1E},
	{0, 0x60, 0x46, 0xA0},
	{0x3AC, 0x60, 0x46, 0xA0},
	{0, 0x60, 0x46, 0xA0},
	{0, 0x60, 0x46, 0xA0},
	{0x3A2, 0xAF, 0x46, 0x1E},
	{0x78, 0x50, 0x50, 0x1E},
	{0x29E, 0x60, 0x3C, 0x1E},
	{0x3A2, 0xAF, 0x46, 0x1E},
	{0x29E, 0x60, 0x3C, 0x1E},
	{0x3A2, 0xAF, 0x46, 0x1E},
	{0x27A, 0xAA, 0x28, 0x14},
	{0, 0xAA, 0x28, 0x3C},
	{0x29E, 0x60, 0x3C, 0x1E},
	{0x3A2, 0xAF, 0x46, 0x1E},
	{0x3AC, 0x60, 0x46, 0xA0},
	{0, 0x7F, 0x32, 0x80},
	{0x154, 0x6E, 0x50, 0x1E},
	{0x1D6, 0xA0, 0x28, 0x14},
	{0x3C0, 0x60, 0x46, 0xA0},
	{0, 0x60, 0x46, 0xA0},
	{0x140, 0x64, 0x3C, 0x14},
	{0x14A, 0x82, 0x64, 0x50},
	{0x52E, 0xBE, 0x14, 0x14},
	{0x29E, 0x60, 0x3C, 0x1E},
	{0x3A2, 0xAF, 0x46, 0x1E},
	{0x6D6, 0x64, 0x64, 0x28},
	{0x7BA, 0xB4, 0x46, 0x50},
	{0x6D6, 0x64, 0x64, 0x28},
	{0x82, 0x50, 0x32, 0x1E},
	{0x3AC, 0x60, 0x46, 0xA0},
	{0x3A2, 0xAF, 0x46, 0x1E},
	{0x3C0, 0x60, 0x46, 0xA0},
	{0, 0x60, 0x46, 0xA0},
	{0x5AC, 0x60, 0x46, 0xA0},
	{0x7BA, 0xB4, 0x46, 0x50},
	{0x144, 0x64, 0x64, 0x3C},
	{0, 0x64, 0x78, 0x3C},
	{0, 0x60, 0x46, 0xA0},
	{0x1DD, 0x65, 0x25, 0x37},
	{0x260, 0x60, 0x32, 0xA},
	{0x15A, 0x50, 0x3B, 0x21},
	{0x1D2, 0x99, 0x20, 0x39},
	{0x172, 0x66, 0x39, 0x43},
	{0x172, 0x66, 0x39, 0x43},
	{0x303, 0x44, 0x5A, 0x10},
	{0x3D4, 0x64, 0x2C, 0x29},
	{0x3D4, 0xB5, 0x2C, 0x28},
	{0x113, 0x5C, 0x26, 0xC},
	{0x464, 0xF0, 0x50, 0x10},
	{0x7CE, 0x60, 0x32, 0xA0},
	{0x3CF, 0x5F, 0x30, 0x3F},
	{0x3D4, 0x20, 0x20, 0x99},
	{0x128, 0xF5, 0x50, 0xB},
	{0x3D4, 0x60, 0x28, 0xA0},
	{0x232, 0x64, 0x3C, 0x10},
	{0x666, 0x26, 0x3F, 0x23},
	{0x74E, 0x63, 0x1E, 0x57},
	{0x537, 0x60, 0x2B, 0x10},
	{0x343, 0x68, 0x2C, 0x10},
	{0x137, 0x59, 0x32, 0x14},
	{0x7A2, 0x82, 0x46, 0x95},
	{0, 0x60, 0x28, 0xA0},
	{0x20, 0xF0, 0x50, 0x10},
	{0x86, 0x4E, 0x2E, 0x14},
	{0xDE, 0xDF, 0x28, 0x41},
	{0x2E5, 0xE2, 0x28, 0x3E},
	{0x2E5, 0xE2, 0x28, 0x3E},
	{0, 0x96, 0x28, 0x6A},
	{0x1D6, 0x60, 0x28, 0xA0},
	{0x3D4, 0x64, 0x2C, 0x29},
	{0x3D4, 0xB5, 0x2C, 0x28},
	{0x7DA, 0xA0, 0x14, 0x50},
	{0x35C, 0x82, 0x14, 0xA},
	{0x2E3, 0xD8, 0x46, 0x50},
	{0x1C4, 0x96, 0x32, 0x14},
	{0x2E3, 0xD8, 0x46, 0x50},
	{0x35C, 0x82, 0x14, 0xA},
	{0x1C4, 0x96, 0x32, 0x14},
	{0x2E3, 0xD8, 0x46, 0x50},
	{0x2E3, 0xD8, 0x46, 0x50},
	{0x15E, 0xF0, 0xB4, 0xA},
	{0x2E3, 0xD8, 0x46, 0x50},
	{0x2E3, 0xD8, 0x46, 0x50},
	{0x11C, 0x77, 0x36, 0x12},
	{0x5D0, 0x70, 0x46, 0x90},
	{0x2CE, 0x42, 0x43, 0x45},
	{0x387, 0x36, 0x3A, 0x58},
	{0x7D0, 0x88, 0x46, 0x78},
	{0, 0xB7, 0x3C, 0x46},
	{0x174, 0x59, 0x5C, 0x3D},
	{0, 0xB7, 0x3C, 0x46},
	{0, 0x79, 0x2D, 0x8E},
	{0x73E, 0x168, 0xAB, 0x2D},
	{0x4FF, 0x1B, 0x52, 0x37},
	{0x1A0, 0x4E, 0x5B, 0x13},
	{0x1A0, 0x4E, 0x5B, 0x13},
	{0x6E, 0x53, 0x41, 0x18},
	{0x1A0, 0x4E, 0x5B, 0x13},
	{0xB0, 0xFA, 0x1E, 0xA},
	{0x651, 0xC0, 0x28, 0x20},
	{0x7D0, 0x121, 0x4F, 0x83},
	{0x651, 0xC0, 0x28, 0x20},
	{0, 0xD2, 0x28, 0x27},
	{0, 0xD2, 0x28, 0x27},
	{0, 0xB7, 0x28, 0x46},
	{0x1D6, 0xB4, 0x1E, 0x50},
	{0, 0xB7, 0x28, 0x46},
	{0x6E, 0x53, 0x41, 0x18},
	{0x1D6, 0x123, 0x14, 0x5C},
	{0x22, 0xF7, 0x1E, 7},
	{0x1D6, 0x123, 0x14, 0x5C},
	{0xE0, 0xF7, 0x1E, 7},
	{0x4FF, 0x1B, 0x52, 0x37},
	{0, 0xB7, 0x28, 0x46},
	{0x1D6, 0xB4, 0x1E, 0x50},
	{0, 0xB7, 0x28, 0x46},
	{0, 0xB7, 0x28, 0x46},
	{0x1D6, 0xB4, 0x1E, 0x50},
	{0, 0xB7, 0x28, 0x46},
	{0x32, 0x73, 0x3E, 0x17},
	{0x7B1, 0x5F, 0x2A, 0x4A},
	{0x258, 0x190, 0x90, 0x21},
	{0x21F, 0xBC, 0x64, 0x27},
	{0x258, 0x190, 0x90, 0x21},
	{0x7A5, 0xE7, 0x3C, 0x2D},
	{0x7D2, 0x116, 0x3A, 0x78},
	{0x21F, 0xBC, 0x64, 0x27},
	{0x7DA, 0x68, 0x32, 0xC4},
	{0x532, 0x74, 0x44, 0x13},
	{0x138, 0x6C, 0x3A, 0x14},
	{0x2BC, 0xF0, 0x28, 0xC},
	{0x2AD, 0x46, 0x28, 0x16},
	{0x2AD, 0x46, 0x28, 0x16},
	{0x2BC, 0xF0, 0x28, 0xC},
	{0x7D0, 0x88, 0x49, 0x78},
	{0x2B, 0x8B, 0x1A, 0x4D},
	{0x30, 0x4E, 0x29, 0x40},
	{0x30, 0x4E, 0x29, 0x40},
	{0x2B, 0x8B, 0x1A, 0x4D},
	{0x30, 0x4E, 0x29, 0x40},
	{0, 0x60, 0x46, 0xA0},
	{0, 0xBA, 0x46, 0xA0},
};


void hook_UseItem(RoroMem *a1,char a2,int a3,int a4,int a5,int a6){
	//FUNC32(DU32(0x2ecf8e + api_getarm(0xa5,a1->UseItem)*4))(a1,a3,a4,a5,a6);
	//FUNC32(DU32(0x2ecf8e + api_getarm(0xa5,0x28)*4))(a1,a3,a4,a5,a6);
	FUN_00194c0e(a1,a3,a4,a5);
	
	return;
}



const struct DoorInfo NewDoor[]= {
	{0x347, 0xF0, 0x24, 0x3A,0x1dd13a,6,0,0,0,-1,0},//x,y,w,h,func,go,item,score,lv,role,players
	{0x182, 0xF4, 0x50, 0x0c,0x1e0402,4,0,0,0,-1,0},//x,y,w,h,func,go,item,score,lv,role,players
};



static struct DoorInfo DoorList[2]= {

};


//检查道具条件
int ChkItem(char ItemID) {
	int i;
	for(i=0;i<4;i++){
		if(FUN_00161054(0x8114f4 + i * 438 ,ItemID))
			return 1;
	}
	return 0;
}
//检查分数条件
int ChkScore(int NeedScore) {
	int i;
	Print(0,6,8,0,0,"score=%d",DU32(0x811bcc+0*732+10));
	for(i=0;i<4;i++){
		if((gRoleMem[i].Active == 2) && DU32( 0x811bcc + i * 732 + 10)>=NeedScore){
			Print(0,20,8,0,0,"%d",i);
			return 1;
		}
	}
	return 0;
}
//检查等级条件
int ChkLevel(char NeedLevel) {
	int i;
	Print(0,6,9,0,0,"level=%d",DU16(0x811bcc+0*732+0x2ba));
	for(i=0;i<4;i++){
		if((gRoleMem[i].Active == 2) && DU16(0x811bcc+ i * 732 + 0x2ba)>=NeedLevel){
			Print(0,20,9,0,0,"%d",i);
			return 1;
		}
			
	}
	return 0;
}
//检查角色条件
int ChkRole(char RoleID) {
	int i;
	Print(0,6,10,0,0,"RoleID=%d",gRoleMem[0].RoroRom->RoleId);
	for(i=0;i<4;i++){
		if(gRoleMem[i].RoroRom->RoleId == RoleID)
			return 1;
	}
	return 0;
}
//检查角色条件
int ChkPlayers(char Players) {
	Print(0,6,11,0,0,"Players=%d",DU8(0x8129ac));
	return (DU8(0x8129ac)>=Players);
}


//根据ID来创建门，可设置条件
void SetDoorByID(int DoorID,int DoorAxilID) {

	if(NewDoor[DoorAxilID].ChkHasItem > 0 && !ChkItem(NewDoor[DoorAxilID].ChkHasItem))//道具条件判断
		return;
	if(NewDoor[DoorAxilID].ChkScore > 0 && !ChkScore(NewDoor[DoorAxilID].ChkScore))//分数条件判断
		return;
	if(NewDoor[DoorAxilID].ChkLevel > 0 && !ChkLevel(NewDoor[DoorAxilID].ChkLevel))//等级条件判断
		return;
	if(NewDoor[DoorAxilID].ChkRole >= 0 && !ChkRole(NewDoor[DoorAxilID].ChkRole))//角色条件判断
		return;
	if(NewDoor[DoorAxilID].ChkPlayers > 0 && !ChkPlayers(NewDoor[DoorAxilID].ChkPlayers))//玩家数条件判断
		return;	
	Print(0,6,12,0,0,"setdoor ok");
	DoorList[DoorID] = NewDoor[DoorAxilID];

	GoMessage_00196142(DoorID,DoorList[DoorID].x,DoorList[DoorID].y,DoorList[DoorID].Go,0);
	return;
}






/*设置一个门*/
void api_setdoor(int DoorID,int DoorAxilID,int Func) {
	DoorList[DoorID].x = DoorTbl[DoorAxilID][0];
	DoorList[DoorID].y = DoorTbl[DoorAxilID][1];
	DoorList[DoorID].w = DoorTbl[DoorAxilID][2];
	DoorList[DoorID].h = DoorTbl[DoorAxilID][3];
	DoorList[DoorID].Func = Func;
}

void api_clrdoor(int DoorID) {
	DoorList[DoorID].x=0;
	DoorList[DoorID].y=0;
	DoorList[DoorID].w=0;
	DoorList[DoorID].h=0;
	DoorList[DoorID].Func=0;
	return;
}

void api_clralldoor() {
	DoorList[0].Func = 0;
	DoorList[1].Func = 0;
}

/*检查玩家是否进门*/
void api_chkdoor(int PlayerMem) {
	int i;
	int x;
	int y;
	x = DU16(PlayerMem + 0x14);
	y = DU16(PlayerMem + 0x16);
	for(i=0; i<2; i++) {
		//Print(0,4,i+13,0,0,"Door%d=%X X=%d Y=%d W=%d H=%d    ",i+1,DoorList[i].Func,DoorList[i].x,DoorList[i].y,DoorList[i].w,DoorList[i].h);
		if(DoorList[i].Func!=0) {

			if(DoorList[i].x <= x && DoorList[i].x+DoorList[i].w >= x) { //这里确定了x在范围内
				if(DoorList[i].y<=y && DoorList[i].y+DoorList[i].h>=y) { //这里确定了y也在范围内
					if((DU8(PlayerMem+0x188)&4)!=0 || (DU8(PlayerMem+0xd3)&4)!=0) {
						DU16(PlayerMem+0x184) = 0;
						DU16(PlayerMem+0x19c) = 0;
						return;
					}
					FUNC32(DoorList[i].Func)(PlayerMem);
				}
			}
		}
	}
	return;
}